package com.example.service;

import com.alibaba.fastjson.JSON;
import com.example.entity.Order;
import com.example.repository.StockRepository;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shenxiang
 * @date 2020/11/14 21:36
 * @description
 */
@Service
@RocketMQMessageListener(topic = "seckill_queue_await_pay",consumerGroup = "seckill_queue_await_pay_grp")
public class payTimeoutConsumer implements RocketMQListener<Order> {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StockRepository stockRepository;

    @Override
    public void onMessage(Order order) {
        System.out.println("等待支付队列收到了消息:"+JSON.toJSONString(order));
        if (!stockRepository.existsOrderNo(order.getOrderNo())){
            // 订单不存在
            System.out.println("订单不存在");
            return;
        }
        // 订单还未支付的情况下,设置为已取消状态
        if (stockRepository.existsPayedByOrderNo(order.getOrderNo())){
            System.out.println("订单:"+order.getOrderNo()+"已经支付过了.");
        }else{
            orderService.setCancelByOrderNo(order.getOrderNo());
        }
    }
}
